package com.whsr.readservice.dao;

import com.whsr.readservice.dto.ChapterContentRequest;
import com.whsr.readservice.vo.Chapter;
import com.whsr.readservice.vo.ChapterContent;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ChapterContentMapper {

    @Insert("<script>" +
            "insert into chapter_content (ch_id, book_id, ch_content, " +
            " create_time, update_time)" +
            "values " +
            "<foreach collection =\"chapterContents\" item=\"chapterContent\" index= \"index\" separator =\",\">" +
            "(#{chapterContent.chId}, #{chapterContent.bookId}, #{chapterContent.chContent}," +
            " now(), now())" +
            "</foreach>" +
            "</script>")
    int insertChapterContents(@Param("chapterContents") List<ChapterContent> chapterContents);

    @Insert("insert into chapter_content (ch_id, book_id, ch_content, " +
            " create_time, update_time)" +
            "values " +
            "(#{chId}, #{bookId}, #{chContent}," +
            " now(), now())")
    int insertChapterContent(ChapterContent chapterContent);

    @Select("<script>" +
            "select" +
            " a.ch_id as chId, " +
            " a.book_id as bookId, " +
            " b.ch_name as chName, " +
            " b.ch_charge as chCharge, " +
            " a.ch_content as chContent " +
            "from chapter_content a, chapter b " +
            "where a.book_id = b.book_id " +
            " and a.ch_id = b.ch_id " +
            " and a.book_id = #{bookId} " +
            "<when test='chId != 0'>" +
            " and a.ch_id = #{chId}" +
            "</when>" +
            "<when test='chId == 0'>" +
            " and b.ch_index=0 " +
            "</when>" +
            "</script>")
    ChapterContentRequest selectContentByBookIdAndChIdOrNoChId(@Param("bookId") Integer bookId, @Param("chId") Integer chId);

    @Select("select" +
            " a.ch_id as chId, " +
            " a.book_id as bookId, " +
            " b.ch_name as chName, " +
            " b.ch_charge as chCharge, " +
            " a.ch_content as chContent " +
            "from chapter_content a, chapter b " +
            "where a.book_id = b.book_id " +
            " and a.ch_id = b.ch_id " +
            " and b.ch_index=0 " +
            " and b.book_id = #{bookId}")
    ChapterContentRequest selectContentByBookIdAndNoChId(Integer bookId);
}
